﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UCL;
using UCL.Data;
using UCL.Forms;

namespace DBKit5
{
    public partial class frmModuleDataTypePicker : Form
    {
        public string DataTypeName { get; set; }
        public string Length { get; set; }
        public string DbProviderName { get; set; }
        GeneralDataTypeHelper gdth = new GeneralDataTypeHelper();
        public frmModuleDataTypePicker()
        {
            InitializeComponent();
        }

        private void frmModuleDataTypePicker_Load(object sender, EventArgs e)
        {
            SetRadioButtonClickEvent();
            if (!string.IsNullOrEmpty(DataTypeName))
            {
                string txt = gdth.GetGeneralDbTypeName(DataTypeName, DbProviderName);
                /*
                string txt = this.DataTypeName;
                int i = DataTypeName.IndexOf('(');
                if (i > -1)
                {
                    txt = this.DataTypeName.Substring(0, i);
                    tbLength.Text = this.DataTypeName.Substring(i + 1).TrimEnd(')');
                }*/
                RadioButton rb;
                foreach (Control c in this.Controls)
                {
                    rb = c as RadioButton;
                    if (rb == null) continue;
                    if (rb.Text == txt)
                    {
                        rb.Checked=true;
                        break;
                    }
                }
                if (!string.IsNullOrEmpty(Length)) tbLength.Text = Length;
            }
        }
        private void CreateRadioButtons()
        {

        }

        private void SetRadioButtonClickEvent()
        {
            foreach (Control c in this.Controls)
            {
                if (c.GetType() != typeof(RadioButton)) continue;
                ((RadioButton)c).Click += new EventHandler(rbType_Click);
            }
        }
        private void rbType_Click(object s, EventArgs e)
        {
            string typeName=((RadioButton)s).Text;
            typeName=gdth.GetProductDbTypeName(typeName,DbProviderName);
            if (typeName.Contains('%'))
            {
                tbLength.Enabled = true;
                tbCode.Text = typeName.TrimEnd('%');
                tbLength.Text = "%";
            }
            else
            {
                tbLength.Clear();
                tbLength.Enabled = false;
                tbCode.Text = typeName;
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (tbCode.TextLength==0)
            {
                MessageBox.Show("未选择任何类型");
                return;
            }
            this.RefreshOwner<frmModuleFieldEdit>("SetTypeValue", tbCode.Text);
            this.Close();
        }
    }
}
